package com.kbtxwer.tmss.URLsupport;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class GetPostUtil {
    public static String sendGet(String url,String params){
        return sendGet(url,params,"utf-8");
    }

    public static String sendGet(String url, String params, String codeType){
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try{
            String urlName = url + "?" + params;
            URL realURL = new URL(urlName);
            URLConnection conn = realURL.openConnection();
            conn.setRequestProperty("accept","*/*");
            conn.setRequestProperty("connection","Keep-Alive");
            conn.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36");
            conn.connect();

            Map<String, List<String>> map = conn.getHeaderFields();
//            for(String key:map.keySet()){
//                System.out.println(key + "--->" + map.get(key));
//            }
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),codeType));
            String line;
            while((line = in.readLine())!=null){
                result.append(line).append("\n");
            }
        }catch (Exception e){
            System.out.println("GET请求异常" + e);
            e.printStackTrace();
        }
        finally {
            try{
                if(in!=null) in.close();
            } catch (IOException ex){
                ex.printStackTrace();
            }
        }
        return result.toString();
    }
    public static String sendPost(String url,String params){
        return sendPost(url,params,"utf-8");
    }

    public static String sendPost(String url,String params,String codeType){
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        try{
            URL realURL = new URL(url);
            URLConnection conn = realURL.openConnection();
            conn.setRequestProperty("accept","*/*");
            conn.setRequestProperty("connection","Keep-Alive");
            conn.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(params);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),codeType));
            String line;
            while((line=in.readLine())!=null){
                result.append(line).append("\n");
            }
        }catch(Exception e){
            System.out.println("POST请求异常" + e);
            e.printStackTrace();
        }
        finally {
            try{
                if(in!=null) in.close();
                if(out!=null) out.close();
            } catch (IOException ex){
                ex.printStackTrace();
            }
        }
        return result.toString();
    }
}
